﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace Fotobanka
{
    /// <summary>
    /// Trida obsahuje podpurne funkce
    /// </summary>
    public static class Utils
    {
        /// <summary>
        /// SystemRoles 
        /// Dostupne role v systemu
        /// </summary>
        public enum SystemRoles
        {
            Admin=1,Spravce=2,Uzivatel=3
        }
        /// <summary>
        /// Seznam webovych stranek s nastavenim jejich pristupovych prav
        /// </summary>
        private static PageAccesInfo []pageInfo = {
                                                      new PageAccesInfo("memberarea.aspx",true,true,true),
                                                      new PageAccesInfo("listgalerii.aspx",true,true,true),
                                                      new PageAccesInfo("pridatgalerii.aspx",true,true,false),
                                                      new PageAccesInfo("spravaskupin.aspx",true,true,false),
                                                      new PageAccesInfo("editovatgalerii.aspx",true,true,false),
                                                      new PageAccesInfo("listskupin.aspx",true,true,false),
                                                      new PageAccesInfo("pridatskupinu.aspx",true,true,false),
                                                      new PageAccesInfo("editovatskupinu.aspx",true,true,false),
                                                      new PageAccesInfo("listuzivatelu.aspx",true,false,false),
                                                      new PageAccesInfo("editovatuzivatele.aspx",true,false,false),
                                                      new PageAccesInfo("spravauzivatelu.aspx",true,true,false),
                                                      new PageAccesInfo("pridatfotografii.aspx",true,true,false),
                                                      new PageAccesInfo("spravafotek.aspx",true,true,false),
                                                      new PageAccesInfo("ukazgalerie.aspx",true,true,true),
                                                      new PageAccesInfo("ukazgalerii.aspx",true,true,true),
                                                      new PageAccesInfo("ukazfoto.aspx",true,true,true),
                                                      new PageAccesInfo("spravauzivateluvgalerii.aspx",true,true,false)
                                                  };
        public static string FotoFolder = "Fotos";
        public static string ThumbExtension = "_thumb";

        /// <summary>
        /// Kontrola jestli muze uzivatel ke strance pristoupit
        /// </summary>
        /// <param name="pagename">nazev stranky</param>
        /// <param name="role">role uzivatele</param>
        /// <returns>true-muze, false-nemuze</returns>
        public static bool CanAccess(string pagename, SystemRoles role)
        {
            foreach (PageAccesInfo s in pageInfo)
            {
                if (s.Page == pagename.ToLower())
                {
                    switch ((int)role)
                    {
                        case 1:
                            if (s.AdminAccess == true)
                            {
                                return true;
                            }
                            return false;
                        case 2:
                            if (s.SpravceAccess == true)
                            {
                                return true;
                            }
                            return false;
                        case 3:
                            if (s.UzivatelAccess == true)
                            {
                                return true;
                            }
                            return false;
                        default:
                            return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// Kontrola jestli je stranka ve clenske casti
        /// </summary>
        /// <param name="pagename">nazev stranky</param>
        /// <returns>true-je, false-neni</returns>
        public static bool IsPageInMembersArea(string pagename)
        {
            foreach (PageAccesInfo s in pageInfo)
            {
                if (s.Page == pagename.ToLower())
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Vrazi MD5 hash predaneho textu (hesla)
        /// </summary>
        /// <param name="text">retezec</param>
        /// <returns>md5 hash</returns>
        public static string GetMd5Hash(string text)
        {
            byte[] pass = Encoding.UTF8.GetBytes(text);
            MD5 md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(pass);
            StringBuilder sb = new StringBuilder(md5.HashSize * 2);
            foreach (byte b in md5.Hash)
            {
                sb.AppendFormat("{0:x2}", b);
            }

            return sb.ToString();
        }
    
        /// <summary>
        /// Vraci nazev uzivatelske role podle jeji id
        /// </summary>
        /// <param name="n">id role</param>
        /// <returns>nazev role</returns>
        public static string GetSystemRoleString(int n)
        {
            switch (n)
            {
                case (int)SystemRoles.Admin:
            	    return "Admin";
                case (int)SystemRoles.Spravce:
                    return "Spravce";
                default:
                    return "Uzivatel";
            }
        }
    }
}
